//
//  UserSymptomCell.swift
//  TCMHealth
//
//  Created by 韩颖 on 2023/7/10.
//

import UIKit
import Foundation
import HXPHPicker
import IQKeyboardManager


class UserSymptomCell: UITableViewCell {
    
    @IBOutlet weak var photoContentView: UIView!
    @IBOutlet weak var symptomTextView: UITextView!
    @IBOutlet weak var photoContentHeight: NSLayoutConstraint!
    @IBOutlet weak var desTF: IQTextView!
    @IBOutlet weak var diagnoseTF: IQTextView!
    
    var photoView: SinaPhotoView!
    var assetArray = [PhotoAsset]()
    var imageUrlArray:[String] = []
    var updateCell: (() -> Void)?
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        self.photoView = SinaPhotoView.init(frame: CGRect(x: 20, y: 10, width: photoContentView.width - 40, height: 90))
        self.photoContentView.addSubview(photoView)
        self.photoView.model = .LocalEdit
        self.photoView.deleteBtnClosure = { [unowned self] index , model ,imageView in
            assetArray.remove(at: index)
            updatePhotoView()
            self.updateCell?()
        }
        
        photoView.clickClosure = { [unowned self] index , imageView ,imageModel in
            print("clickIndex = ", index)
            presentPreViewController(currentIndex: index)
            
        }
        
        
    }
    func updatePhotoView() {
        photoContentView.isHidden = assetArray.count == 0
        photoContentHeight.constant = assetArray.count == 0 ? 0 : 110
    }
    
    func setMedicines() {
        
    }
    
    
    @IBAction func addPhoto(_ sender: Any) {
        self.presentPickerController()
    }
    
//    func getUserInfo(userInfo: UserInfo) {
//        userInfo. = desTF.text ?? ""
//        
//    }
    
    func presentPickerController() {
        // 设置与微信主题一致的配置
        let config = PhotoTools.getWXPickerConfig()
        config.maximumSelectedCount = 4
        let pickerController = PhotoPickerController(picker: config)
        pickerController.pickerDelegate = self
        // 当前被选择的资源对应的 PhotoAsset 对象数组
        pickerController.selectedAssetArray = assetArray
        // 是否选中原图
        pickerController.isOriginal = false
        self.getViewOnController().present(pickerController, animated: true, completion: nil)
    }
    
    
    func presentPreViewController(currentIndex: Int) {
        let config = PickerConfiguration.init()
        let preViewController =
        PhotoPickerController(preview: config, currentIndex: currentIndex, modalPresentationStyle: .overFullScreen, delegate: self)
        preViewController.selectedAssetArray = self.assetArray
        self.getViewOnController().present(preViewController, animated: true, completion: nil)
    }
}

    
    
    extension UserSymptomCell: PhotoPickerControllerDelegate {
    
        /// 选择完成之后调用
        /// - Parameters:
        ///   - pickerController: 对应的 PhotoPickerController
        ///   - result: 选择的结果
        ///     result.photoAssets  选择的资源数组
        ///     result.isOriginal   是否选中原图
        func pickerController(_ pickerController: PhotoPickerController,
                                didFinishSelection result: PickerResult) {
            result.getImage { (image, photoAsset, index) in
                if let image = image {
                    print("success", image)
                    if !self.assetArray.contains(photoAsset) {
                        self.assetArray.append(photoAsset)
                        self.imageUrlArray.append(photoAsset.localImageAsset?.imageURL!.absoluteString ?? "")
                        self.photoView.addPhotoModels(photoModels: [SinaPhotoView.PhotoModel(img: image, interfaceModel: nil)])
                    }
                }else {
                    print("failed")
                }
            } completionHandler: { (images) in
                self.updatePhotoView()
                self.updateCell?()
            }
        }
    
        /// 点击取消时调用
        /// - Parameter pickerController: 对应的 PhotoPickerController
        func pickerController(didCancel pickerController: PhotoPickerController) {
    
        }
    
}

